package com.xhx.accounting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class loan extends AppCompatActivity {
    private EditText money,month;
    private Button add_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan);
        money = findViewById(R.id.money);
        month = findViewById(R.id.loan_month);
        add_button = findViewById(R.id.add_button);
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int x = Integer.parseInt(money.getText().toString()) * 10000;
                int y = Integer.parseInt(month.getText().toString());
                Map<String,Object> map = calculate(x,y);
                Log.d("mapppppp",map.toString());
                Intent intent = new Intent();
                intent.setClass(loan.this, loan_result.class);
                intent.putExtra("loan_result", (Serializable) map);
                startActivity(intent);
            }
        });
    }

    private Map<String,Object> calculate(int money,int month){
        double rate = 0.049 / 12;
        Map result = new HashMap();
        double everyMonthTotal = money * (rate * Math.pow((1 + rate),month)) / (Math.pow((1 + rate),month)-1);
        double totalInterest = month * everyMonthTotal - money;
        result.put("everyMonthTotal",everyMonthTotal);
        result.put("totalInterest",totalInterest);

        List<Map<String,Object>> list = new ArrayList();
        for(int i = 1 ; i <= month ; i = i + 1){
            Map<String, Object> map = new HashMap<>();
            double everyMonthMoney = money * rate * Math.pow((1 + rate),(i - 1)) / (Math.pow((1 + rate),month)-1);
            double everyMonthInterest = everyMonthTotal - everyMonthMoney;
            map.put("month",i);
            map.put("everyMonthInterest",everyMonthInterest);
            map.put("everyMonthMoney",everyMonthMoney);
            map.put("everyMonthTotal",everyMonthTotal);
            list.add(map);
        }
        result.put("month",month);
        result.put("money",money);
        result.put("everyMonthData",list);
        return result;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /*
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /*
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_left:
                finish();
                break;
        }
    }
}